package com.example.springbootdoc.controller;

import com.example.springbootdoc.entity.User;
import com.example.springbootdoc.utils.WordUtils;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "WORD文件下載")
@RestController
@RequestMapping("/word")
public class WordController {

    @ApiOperation("文件下载")
    @GetMapping("/downloadWord")
    public void downloadWord(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> list = new ArrayList<>();
        list.add(new User("王小鱼", "三(1)班", "1380000000", "北京市朝阳区幸福小区101号", "男"));
        list.add(new User("李一一", "三(3)班", "1360000000", "北京市朝阳区汤臣一品61号", "女"));

        Map<String, Object> map = new HashMap<>();
        map.put("users", list);
        map.put("date", "2023年1月2日");
        map.put("company", "保护伞公司");

        String wordName = "addr.ftl";
        String fileName = "通讯录.doc";
        String name = "test";
        System.out.println(new Gson().toJson(map));
        WordUtils.exportMillCertificateWord(request, response, map, wordName, fileName, name);
    }
}
